<template>
  <el-dialog
    :title="!dataForm.rentLogId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="租房id" prop="rentId">
      <el-input v-model="dataForm.rentId" placeholder="租房id"></el-input>
    </el-form-item>
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="rentStartTime">
      <el-input v-model="dataForm.rentStartTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="到期时间" prop="rentEndTime">
      <el-input v-model="dataForm.rentEndTime" placeholder="到期时间"></el-input>
    </el-form-item>
    <el-form-item label="是否有效（0 无，1 有）" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="是否有效（0 无，1 有）"></el-input>
    </el-form-item>
    <el-form-item label="支付金额" prop="price">
      <el-input v-model="dataForm.price" placeholder="支付金额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentLogId: 0,
          rentId: '',
          userId: '',
          rentStartTime: '',
          rentEndTime: '',
          flag: '',
          price: ''
        },
        dataRule: {
          rentId: [
            { required: true, message: '租房id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          rentStartTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          rentEndTime: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '是否有效（0 无，1 有）不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '支付金额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentLogId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentLogId) {
            this.$http({
              url: this.$http.adornUrl(`/house/rentlog/info/${this.dataForm.rentLogId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rentId = data.rentLog.rentId
                this.dataForm.userId = data.rentLog.userId
                this.dataForm.rentStartTime = data.rentLog.rentStartTime
                this.dataForm.rentEndTime = data.rentLog.rentEndTime
                this.dataForm.flag = data.rentLog.flag
                this.dataForm.price = data.rentLog.price
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/rentlog/${!this.dataForm.rentLogId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentLogId': this.dataForm.rentLogId || undefined,
                'rentId': this.dataForm.rentId,
                'userId': this.dataForm.userId,
                'rentStartTime': this.dataForm.rentStartTime,
                'rentEndTime': this.dataForm.rentEndTime,
                'flag': this.dataForm.flag,
                'price': this.dataForm.price
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
